package com.sf.fy.uav.applet.banner.controller;

import com.sf.fy.uav.applet.banner.service.SysBannerService;
import com.sf.fy.uav.common.utils.SfOssClientUtil;
import com.sf.fy.uav.common.request.common.IdRequest;
import com.sf.fy.uav.common.request.operate.FileAddRequest;
import com.sf.fy.uav.common.request.operate.SysBannerAddRequest;
import com.sf.fy.uav.common.request.operate.SysBannerEditRequest;
import com.sf.fy.uav.common.request.operate.SysBannerRequest;
import com.sf.fy.uav.common.request.order.DealRequest;
import com.sf.fy.uav.common.response.operational.BannerPictureResponse;
import com.sf.fy.uav.common.response.operational.SysBannerResponse;
import com.sf.fy.uav.common.utils.PhoneEncoderUtil;
import com.sf.fy.uav.common.utils.Rs;
import com.sf.fy.uav.common.utils.UploadToOssUtils;
import com.sf.fy.uav.controller.FileRest;
import com.sf.fy.uav.rest.BannerRest;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;


/**
 * @author LTQ
 */
@RestController
@RequestMapping("/home/banner/")
@Tag(name = "banner管理")
public class SysBannerController {

    private final static Logger logger = LoggerFactory.getLogger(SysBannerController.class);
    @Autowired
    private BannerRest bannerRest;

    @Autowired
    private SysBannerService iBannerService;

    @Autowired
    private UploadToOssUtils uploadToOssUtils;

    @Autowired
    private FileRest fileRest;

    @GetMapping("get-banner")
    @CrossOrigin
    public Rs<List<SysBannerResponse>> getBanners(@RequestParam Integer page, @RequestParam Integer size, @RequestParam(required = false) String name, @RequestParam(required = false) Integer status) {
        SysBannerRequest sysBannerRequest = new SysBannerRequest();
        sysBannerRequest.setPage(page);
        sysBannerRequest.setSize(size);
        sysBannerRequest.setStatus(status);
        sysBannerRequest.setName(name);
        return Rs.success(bannerRest.getBanners(sysBannerRequest));
    }

    @GetMapping("get-total")
    @CrossOrigin
    public Rs<Long> getTotal(@RequestParam(required = false) String name, @RequestParam(required = false) Integer status) {
        return Rs.success(bannerRest.getTotal(name, status));
    }

    @PostMapping("add")
    @CrossOrigin
    public Rs<Void> add(@RequestBody SysBannerAddRequest sysBannerAddRequest) {
        iBannerService.add(sysBannerAddRequest);
        return Rs.success();
    }

    @PostMapping("edit")
    @CrossOrigin
    public Rs<Void> edit(@RequestBody SysBannerEditRequest sysBannerEditRequest) {
        iBannerService.edit(sysBannerEditRequest);
        return Rs.success();
    }

    @PostMapping("off")
    public Rs<Void> off(@RequestBody DealRequest dealRequest) {
        bannerRest.off(dealRequest);
        return Rs.success();
    }

    public Rs<Integer> start(@RequestBody IdRequest idRequest) {
        return Rs.success(bannerRest.start(idRequest));
    }


    public Rs<SysBannerResponse> getById(@RequestParam String bannerId) {
        return Rs.success(bannerRest.getById(bannerId));
    }

    public Rs<Integer> end(@RequestBody IdRequest idRequest) {
        return Rs.success(bannerRest.end(idRequest));
    }

    @PostMapping("delete")
    public Rs<Void> delete(@RequestBody DealRequest dealRequest) {
        bannerRest.delete(dealRequest);
        return Rs.success();
    }

    @PostMapping("picture")
    @CrossOrigin
    @Parameter(name = "subject", description = "取值含义business上传商家图片,commodity上传商品图片")
    public BannerPictureResponse uploadPicture(HttpServletRequest request, HttpServletResponse response, @RequestParam String subject, @RequestParam(required = false) String compressionQuality) throws Exception {
        FileAddRequest fileAddRequest = uploadToOssUtils.uploadPicture(request, response, subject, compressionQuality);
        BannerPictureResponse bannerPictureResponse = new BannerPictureResponse();
        com.sf.fy.uav.entity.File file2 = fileRest.add(fileAddRequest);
        bannerPictureResponse.setFileId(file2.getId());
        bannerPictureResponse.setFilePath(fileAddRequest.getFilePath());
        return bannerPictureResponse;
    }

}
